package com.sugar.subscribe.course.module.business.controller.admin.venue;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;
import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;

import com.sugar.subscribe.course.framework.excel.core.util.ExcelUtils;

import com.sugar.subscribe.course.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.subscribe.course.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.subscribe.course.module.business.controller.admin.venue.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.venue.VenueDO;
import com.sugar.subscribe.course.module.business.service.venue.VenueService;

@Tag(name = "管理后台 - 场地")
@RestController
@RequestMapping("/business/venue")
@Validated
public class VenueController {

    @Resource
    private VenueService venueService;

    @PostMapping("/create")
    @Operation(summary = "创建场地")
    @PreAuthorize("@ss.hasPermission('business:venue:create')")
    public CommonResult<Long> createVenue(@Valid @RequestBody VenueSaveReqVO createReqVO) {
        return success(venueService.createVenue(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新场地")
    @PreAuthorize("@ss.hasPermission('business:venue:update')")
    public CommonResult<Boolean> updateVenue(@Valid @RequestBody VenueSaveReqVO updateReqVO) {
        venueService.updateVenue(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除场地")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:venue:delete')")
    public CommonResult<Boolean> deleteVenue(@RequestParam("id") Long id) {
        venueService.deleteVenue(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除场地")
                @PreAuthorize("@ss.hasPermission('business:venue:delete')")
    public CommonResult<Boolean> deleteVenueList(@RequestParam("ids") List<Long> ids) {
        venueService.deleteVenueListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得场地")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:venue:query')")
    public CommonResult<VenueRespVO> getVenue(@RequestParam("id") Long id) {
        VenueDO venue = venueService.getVenue(id);
        return success(BeanUtils.toBean(venue, VenueRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得所有场地")
    @PreAuthorize("@ss.hasPermission('business:venue:query')")
    public CommonResult<List<VenueSimpleRespVO>> getVenueAll() {
        return success(venueService.getVenueAll());
    }

    @GetMapping("/page")
    @Operation(summary = "获得场地分页")
    @PreAuthorize("@ss.hasPermission('business:venue:query')")
    public CommonResult<PageResult<VenueRespVO>> getVenuePage(@Valid VenuePageReqVO pageReqVO) {
        PageResult<VenueDO> pageResult = venueService.getVenuePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VenueRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出场地 Excel")
    @PreAuthorize("@ss.hasPermission('business:venue:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVenueExcel(@Valid VenuePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VenueDO> list = venueService.getVenuePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "场地.xls", "数据", VenueRespVO.class,
                        BeanUtils.toBean(list, VenueRespVO.class));
    }
}